﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DirectedGraphCycle
{
    public partial class MainForm : Form
    {
        FindAllCycle findAllCyle = null;
        protected int[][] adjacencyListOriginal = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void chooseFile_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Open File";
            DialogResult result = openDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.fileNam_textBox.Text = openDialog.FileName;
            }
        }

        private void running_button_Click(object sender, EventArgs e)
        {
            int start = Environment.TickCount;
            readGraphData();
            if (directed_radioButton.Checked)
            {
                findAllCyle = new FindAllDirectedCycle();
            }
            else
            {
                findAllCyle = new FindAllDirectedCycle();
            }
            findAllCyle.InitGraph(adjacencyListOriginal);
            string allCyle = findAllCyle.printAllCycles();
            this.allCycles_richTextBox.Text = allCyle;
            int duration = Environment.TickCount - start;
            this.runningTime_textBox.Text = duration.ToString();
        }

        private void readGraphData()
        {
            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(this.fileNam_textBox.Text))
                {
                    string line = null;
                    line = file.ReadLine();
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        throw new System.IO.IOException("Empty file");
                    }
                    int numNodes;
                    Int32.TryParse(line, out numNodes);
                    adjacencyListOriginal = new int[numNodes][];
                    int v = 0;
                    int w = 0;
                    while ((line = file.ReadLine()) != null)
                    {
                        string[] adjacencyList = line.Split(' ');
                        adjacencyListOriginal[v] = new int[adjacencyList.Length];
                        for(int i = 0; i < adjacencyList.Length; i++)
                        {
                            Int32.TryParse(adjacencyList[i], out w);
                            adjacencyListOriginal[v][i] = w;
                        }
                        v++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error in buildGraph()", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    
    }
}
